<html>
<head><meta charset="utf-8"><title>RefCell::borrow_mut inside an if let that consumes it · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/RefCell.3A.3Aborrow_mut.20inside.20an.20if.20let.20that.20consumes.20it.html">RefCell::borrow_mut inside an if let that consumes it</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222170424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/RefCell%3A%3Aborrow_mut%20inside%20an%20if%20let%20that%20consumes%20it/near/222170424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andreu Botella <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/RefCell.3A.3Aborrow_mut.20inside.20an.20if.20let.20that.20consumes.20it.html#222170424">(Jan 09 2021 at 13:06)</a>:</h4>
<p>Hi. Is there any way of borrowing mutably a <code>RefCell</code> inside an <code>if let</code> that consumes a reference to it without panicking? Or some other idiomatic way to do something like that?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="mi">42</span><span class="p">));</span><span class="w"></span>

<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">borrow</span><span class="p">().</span><span class="n">as_ref</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">some_condition</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// At this point we've stopped using `inner`, but the borrow of `a`</span>
<span class="w">        </span><span class="c1">// isn't yet dropped, so this panics.</span>
<span class="w">        </span><span class="o">*</span><span class="n">a</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222170524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/RefCell%3A%3Aborrow_mut%20inside%20an%20if%20let%20that%20consumes%20it/near/222170524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andreu Botella <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/RefCell.3A.3Aborrow_mut.20inside.20an.20if.20let.20that.20consumes.20it.html#222170524">(Jan 09 2021 at 13:08)</a>:</h4>
<p>I know I could do this, but it doesn't seem very idiomatic.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">replace_with_none</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">borrow</span><span class="p">().</span><span class="n">as_ref</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">some_condition</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kc">true</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kc">false</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kc">false</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>

<span class="k">if</span><span class="w"> </span><span class="n">replace_with_none</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="n">a</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222171506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/RefCell%3A%3Aborrow_mut%20inside%20an%20if%20let%20that%20consumes%20it/near/222171506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/RefCell.3A.3Aborrow_mut.20inside.20an.20if.20let.20that.20consumes.20it.html#222171506">(Jan 09 2021 at 13:37)</a>:</h4>
<p>How about something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="mi">42</span><span class="p">));</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">borrow</span><span class="p">();</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">as_ref</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">some_condition</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">drop</span><span class="p">(</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="n">a</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222171680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/RefCell%3A%3Aborrow_mut%20inside%20an%20if%20let%20that%20consumes%20it/near/222171680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andreu Botella <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/RefCell.3A.3Aborrow_mut.20inside.20an.20if.20let.20that.20consumes.20it.html#222171680">(Jan 09 2021 at 13:42)</a>:</h4>
<p>I didn't know you could do that. I guess the compiler can tell that <code>inner</code> isn't being used anymore</p>



<a name="222171739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/RefCell%3A%3Aborrow_mut%20inside%20an%20if%20let%20that%20consumes%20it/near/222171739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andreu Botella <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/RefCell.3A.3Aborrow_mut.20inside.20an.20if.20let.20that.20consumes.20it.html#222171739">(Jan 09 2021 at 13:44)</a>:</h4>
<p>Thanks</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>